package com.application.sistercityapplication.gallery;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.Toast;

import com.application.sistercityapplication.main.ActionBarSetElementUtil;
import com.application.sistercityapplication.main.MainSisterCityActivity;
import com.application.sistercityapplication.main.R;

public class GalleryMainActivity extends Activity {
	private BookshelfView bfView;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gallery_main);
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);

		Intent i = new Intent(GalleryMainActivity.this,MainSisterCityActivity.class);
		new ActionBarSetElementUtil(this, R.string.Asian, R.string.galleryText,
				i).createActionBar();

//		bfView = (BookshelfView) findViewById(R.id.bookShelfGrid);
		new DownloadBooksTask(getApplicationContext(), bfView, this).execute();
	}

	class DownloadBooksTask extends
			AsyncTask<String, Void, List<GalleryPhotoItems>> {
		ProgressDialog pDialog;
		private Context context;
		private BookshelfView view;
		public Activity _activity;

		public DownloadBooksTask(Context context, BookshelfView view,
				Activity act) {
			super();
			this.context = context;
			this.view = view;
			this._activity = act;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(GalleryMainActivity.this);
			pDialog.setMessage("Loading image...");
			pDialog.show();
		}

		@Override
		protected List<GalleryPhotoItems> doInBackground(String... params) {
			List<GalleryPhotoItems> listTest = new ArrayList<GalleryPhotoItems>();
			/*
			 * Dummy image gallery
			 */
			GalleryPhotoItems gpi = new GalleryPhotoItems();
			for (int i = 0; i <= 10; i++) {
				gpi.setThumbImageURL("http://media.treehugger.com/assets/images/2011/10/graff-vertical-farm-perspective.jpg");
				listTest.add(gpi);
			}

			return listTest;
		}

		@Override
		protected void onPostExecute(List<GalleryPhotoItems> result) {
			super.onPostExecute(result);
			if (null != pDialog && pDialog.isShowing()) {
				pDialog.dismiss();
			}

			if (result != null) {
//				view.setAdapter(new GridViewImageAdapter(context, result,_activity));
			} else {
				(Toast.makeText(context, "An error occured.", Toast.LENGTH_LONG))
						.show();
			}

		}
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		overridePendingTransition(R.anim.open_main, R.anim.close_next);
		finish();
	}
}
